var $ = require("../../utils/Ajax.js");
Page({
  data: {
    UserName: null,
    UserPhone: null,
    UserAddress: null,
    Default: 1
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "添加收货地址"
    }) 
  },

  onShow: function () {

  },

  // 用户输入姓名
  UserName: function(e){
    var UserName = e.detail.value;
    this.setData({
      UserName: UserName
    })
  },

  // 用户输入手机号码
  UserPhone: function (e) {
    var UserPhone = e.detail.value;
    if (UserPhone.length == 11){
      this.setData({
        UserPhone: UserPhone
      })
    }
  },

  // 用户输入详细地址
  UserAddress: function (e) {
    var UserAddress = e.detail.value;
    this.setData({
      UserAddress: UserAddress
    })
  },

  // 是否为默认地址
  DefaultSwitch: function(e){
    var DefaultSwitch = e.detail.value;
    if (DefaultSwitch){
      this.setData({
        Default: 1
      })
    }else{
      this.setData({
        Default: 0
      })
    }
  },

  // 点击添加地址
  AddAddress: function(){
    var that = this;
    $.Ajax("api/addAddress","POST",{
      name: that.data.UserName,
      mobile: that.data.UserPhone,
      address: that.data.UserAddress,
      isDefault: that.data.Default
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500,
        mask: true
      })
      setTimeout(function(){
        wx.navigateBack({
          delta: 1
        })
      },2000)
    })
  }
})